package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.data.domain.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author shkstart
 * @create 2023-04-02 15:24
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    // 添加医院接口
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        // 获取传过来的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        // 将string数组转换成集合
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(requestParameterMap);
        // 获取singkey
        String sign = (String) stringObjectMap.get("sign");
        // 获取hoscode
        String hoscode = (String) stringObjectMap.get("hoscode");
        System.out.println("hoscode："+hoscode);
        // 通过hoscode查询数据库并返回signkey
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 用MD5进行加密
        String s = MD5.encrypt(signKey);
        // 判断签名是否相同
        if (!sign.equals(s)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        // 显示图片
        String logoData = (String) stringObjectMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        stringObjectMap.put("logoData", logoData);

        // 调用service方法
        hospitalService.save(stringObjectMap);
        return Result.ok();
    }

    // 查询医院
    @PostMapping("hospital/show")
    public Result getHosp(HttpServletRequest request) {
        // 获取传过来的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        // 将string数组转换成集合
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(requestParameterMap);
        // 获取医院编号
        String hoscode = (String) stringObjectMap.get("hoscode");

        // 获取singkey
        String sign = (String) stringObjectMap.get("sign");
        // 通过hoscode查询数据库并返回signkey
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 用MD5进行加密
        String s = MD5.encrypt(signKey);
        // 判断签名是否相同
        if (!sign.equals(s)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        // 调用service
        Hospital hospital = hospitalService.getByHoscode(hoscode);

        return Result.ok(hospital);
    }

    // 上传科室
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        // 获取传过来的医院信息
        Map<String, String[]> requestParameterMap = request.getParameterMap();
        // 将string数组转换成集合
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(requestParameterMap);
        // 获取singkey
        String sign = (String) stringObjectMap.get("sign");
        // 获取hoscode
        String hoscode = (String) stringObjectMap.get("hoscode");
        System.out.println("hoscode："+hoscode);
        // 通过hoscode查询数据库并返回signkey
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 用MD5进行加密
        String s = MD5.encrypt(signKey);
        // 判断签名是否相同
//        if (!sign.equals(s)) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        // 调用service方法
        departmentService.save(stringObjectMap);
        return Result.ok();
    }

    // 查询科室
    @ApiOperation(value = "获取分页列表")
    @PostMapping("department/list")
    public Result department(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
//非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

//        if(StringUtils.isEmpty(hoscode)) {
//            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
//        }
////签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        departmentQueryVo.setDepcode(depcode);
        Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);

        return Result.ok(pageModel);
    }

    // 删除
    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
//必填
        String depcode = (String)paramMap.get("depcode");
        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    // 上传排班
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        scheduleService.save(paramMap);
        return Result.ok();
    }

    // 查询排班
    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
//非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        if(StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
//        if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        Page<Schedule> pageModel = scheduleService.selectPage(page , limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    // 删除排班
    @ApiOperation(value = "删除科室")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
//必须参数校验 略
        String hoscode = (String) paramMap.get("hoscode");
//必填
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        if (StringUtils.isEmpty(hoscode)) {
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
//签名校验
//        if (!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

}
